package com.qzct.controller;

import com.mybatisflex.core.paginate.Page;
import com.qzct.entity.vo.ResultCode;
import com.qzct.entity.vo.ResultVo;
import com.qzct.service.FileService;
import org.springframework.web.bind.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;
import com.qzct.entity.AttractionImgs;
import com.qzct.service.AttractionImgsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.multipart.MultipartFile;

import java.io.Serializable;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *  控制层。
 *
 * @author 闽闽
 * @since 2025-01-11
 */
@RestController
@Api(tags = "景点图片")
@RequestMapping("/attractionImgs")
public class AttractionImgsController {

    @Autowired
    private AttractionImgsService attractionImgsService;

    /**
     * 添加。
     *
     * @param attractionImgs 
     * @return {@code true} 添加成功，{@code false} 添加失败
     */
    @PostMapping("save")
    @ApiOperation("保存")
    public boolean save(@RequestBody @ApiParam("") AttractionImgs attractionImgs) {
        return attractionImgsService.save(attractionImgs);
    }

    /**
     * 根据主键删除。
     *
     * @param id 主键
     * @return {@code true} 删除成功，{@code false} 删除失败
     */
    @DeleteMapping("remove/{id}")
    @ApiOperation("根据主键")
    public boolean remove(@PathVariable @ApiParam("主键") Serializable id) {
        return attractionImgsService.removeById(id);
    }

    /**
     * 根据主键更新。
     *
     * @param attractionImgs 
     * @return {@code true} 更新成功，{@code false} 更新失败
     */
    @PutMapping("update")
    @ApiOperation("根据主键更新")
    public boolean update(@RequestBody @ApiParam("主键") AttractionImgs attractionImgs) {
        return attractionImgsService.updateById(attractionImgs);
    }

    /**
     * 查询所有。
     *
     * @return 所有数据
     */
    @GetMapping("list")
    @ApiOperation("查询所有")
    public List<AttractionImgs> list() {
        return attractionImgsService.list();
    }

    /**
     * 根据主键获取详细信息。
     *
     * @param id 主键
     * @return 详情
     */
    @GetMapping("getInfo/{id}")
    @ApiOperation("根据主键获取")
    public AttractionImgs getInfo(@PathVariable @ApiParam("主键") Serializable id) {
        return attractionImgsService.getById(id);
    }

    /**
     * 分页查询。
     *
     * @param page 分页对象
     * @return 分页对象
     */
    @GetMapping("page")
    @ApiOperation("分页查询")
    public Page<AttractionImgs> page(@ApiParam("分页信息") Page<AttractionImgs> page) {
        return attractionImgsService.page(page);
    }



    @Autowired
    FileService fileService;
    @PostMapping(value = "/upload/image",consumes = "multipart/form-data")
    @ApiOperation("图片上传测试,返回访问地址")
    public ResultVo imageUploader(@RequestPart(value = "image") MultipartFile image,int id){
        String fileName=image.getOriginalFilename();
        String filepath = fileService.upLoadImg("attraction",fileName,image);
        if(filepath==null){
            return ResultVo.error(ResultCode.FILE_UPLOAD_ERROR);
        }
        AttractionImgs attractionImgs = new AttractionImgs();
        attractionImgs.setAttractionId(id);
        attractionImgs.setUrl(filepath);
        attractionImgsService.save(attractionImgs);
        Map<String,Object> map=new HashMap<>();
        map.put("data",attractionImgs);
        return  new ResultVo(attractionImgs);
    }
}

